﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Celltest
{

    class Program
    {
        //public ConsoleKeyInfo keyinfo = Console.ReadKey(true);
        public const int Timelimit = 100;
        public const bool quit = false;
        static void Main(string[] args)
        {
            //Console.SetWindowSize(Console.,Console.LargestWindowHeight
            //);
            LoadSave ls = new LoadSave();
            string a = "1wwwwwwtwwwwwwwwwww3" + //w:20 h:4
                       "W      W           W" +
                       "W      W           d" +
                       "W      d           W" +
                       "W      W           W" +
                       "W      W           W" +
                       "W      W           W" +
                       "W      W           W" +
                       "W     14           W" +
                       "2wwwwwswwwwwwwwwwww4";
            var t = new List<string>();
            for (int i = 0; i < a.Length; i++)
            {
                var s = a[i].ToString();
                t.Add(s);
            }
            CreateArrayCell c = new CreateArrayCell("test.dat", 20,t);
            var e = new List<Entity>();
            GUI g = new GUI();
            e =ls._Binaryread("test.dat");
            foreach (var entity in c.ent)
            {
                g.WriteAt(entity.B,entity.Pos);
            }
            //for (int i = 0; i < e.Count; i++)
            //{
            //    g.WriteAt(e[i].B,e[i].Pos);
            //    //Console.WriteLine("{0}: byte char:{1}, X{2}, Y{3}",i,e[i].B, e[i].Pos.X,e[i].Pos.Y);
            //}

            //ACRII();
            MainLoop();
            Console.ReadKey();
        }

        /// <summary>
        /// The main gameloop
        /// </summary>
        static ConsoleKeyInfo keyinfo()
        {
            return Console.ReadKey(true);
        }

        static void MainLoop()
        {
            GUI gui = new GUI();
            Cell cell = new Cell();
            int elapsedMilliseconds = 0;
            int totalMilliseconds = Timelimit * 1000;
            const int INTERVAL = 100;
            Position Playerpos = new Position(5, 5);

            Position oldpos = Playerpos;


            //cell.MakeCell(Console.WindowWidth-2, Console.WindowHeight - 2);
            while (elapsedMilliseconds < totalMilliseconds && !quit)
            {
                // Sleep for a short period
                
                //Console.Clear();
                elapsedMilliseconds += INTERVAL;

                var k = keyinfo();
                
                gui.WriteAt(1, Playerpos);

                if (k.Key == ConsoleKey.A)
                {
                    Clear(Playerpos, 1, 1);
                    Playerpos.X --;
                    gui.WriteAt(1, Playerpos);
                }
                else if (k.Key == ConsoleKey.D)
                {
                    Clear(Playerpos, 1, 1);
                    Playerpos.X ++;
                    gui.WriteAt(1, Playerpos);
                    }
                else if (k.Key == ConsoleKey.S)
                {
                    Clear(Playerpos, 1, 1);
                    Playerpos.Y ++;
                    gui.WriteAt(1, Playerpos);
                    }
                else if (k.Key == ConsoleKey.W)
                {
                    Clear(Playerpos, 1, 1);
                    Playerpos.Y --;
                    gui.WriteAt(1, Playerpos);
                }
                else
                {
                    //Clear(oldpos, 1, 1);
                    gui.WriteAt(1, Playerpos);
                }
                //System.Threading.Thread.Sleep(INTERVAL);
                oldpos = Playerpos;
                /*** Update the screen and handle input here! ***/
            }

            Console.WriteLine("Game over!");
        }

        static void Clear(Position pos, int width, int height)
        {
            int curTop = Console.CursorTop;
            int curLeft = Console.CursorLeft;
            var c = Encoding.GetEncoding(437).GetChars(new byte[] { 0 })[0];
            for (; height > 0; )
            {
                Console.SetCursorPosition(pos.X, pos.Y + --height);
                //Console.Write(new string(' ', width));
                Console.Write(c);
                
            }
            Console.SetCursorPosition(curLeft, curTop);
        }

        static void ACRII()
        {
            Console.WriteLine();
            for (byte b = 0; b < byte.MaxValue; b++)
            {
                char c = Encoding.GetEncoding(437).GetChars(new byte[] { b })[0];
                switch (b)
                {
                    case 8: // Backspace
                    case 9: // Tab
                    case 10: // Line feed
                    case 13: // Carriage return
                        c = '.';
                        break;
                }

                Console.Write("{0:000} {1}   ", b, c);

                // 7 is a beep -- Console.Beep() also works
                if (b == 7) Console.Write(" ");

                if ((b + 1) % 8 == 0)
                    Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}
